package com.base.web.auth.service.impl;

import com.base.web.auth.service.RoleService;
import com.base.web.core.basic.service.BasicServiceImpl;
import com.base.web.dao.master.auth.RoleMapper;
import com.base.web.dao.master.auth.RoleResourceMapper;
import com.base.web.model.master.auth.Role;
import com.base.web.model.master.auth.RoleResource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @Author: rentie
 * @Date: 2018/7/10 14:31
 */
@Service
public class RoleServiceImpl extends BasicServiceImpl<Role> implements RoleService {

    @Resource
    private RoleResourceMapper roleResourceMapper;

    @Resource
    private RoleMapper roleMapper;



    /**
     * 角色授权
     *
     * @param roleId
     * @param roleResources
     * @return
     */
    @Override
    @Transactional
    @CacheEvict(value = "menus",key="'userMenu_'+#p0")
    public Integer roleAuthorization(String userId,String roleId, List<RoleResource> roleResources) {

        //删除角色资源关系
        roleResourceMapper.delRoleResByRoleId(roleId);
        //添加新的用户角色关系
        roleResources.forEach(rm -> {
            roleResourceMapper.insertSelective(rm);
        });
        return 1;
    }

    /**
     * 角色管理界面列表查询
     *
     * @return
     */
    @Override
    public PageInfo<Role> queryManageList(Map<String, String> map) {
        Integer currentPage = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("limit"));
        PageHelper.startPage(currentPage, pageSize);
        List<Role> list = roleMapper.queryManageList();
        return new PageInfo<Role>(list);
    }
}